#ifdef TIME_ENABLE
//logMessage(LOGLEVEL_DEBUG, "TIME", "", "");

unsigned long TIMEPreviousMillis;
unsigned long TIMEpreviousSyncMillis;
int TIMEhour = 0;
int TIMEminute = 0;
int TIMEseconds = 0;
unsigned long TIMEtimeSyncPoint = 0;

/*************************************************************
 *
 * globals
 *
 *************************************************************/
 
void setupTime() {
  logMessage(LOGLEVEL_INFO, "TIME", "setup Time", "");
  TIMEtimeSyncPoint = seconds();
  syncTime();
  
  TIMEseconds = 23;
  TIMEminute = 22;
  TIMEhour = 11;
}

String getTimeFormatted() {
  //updateTime();  
  char tempBuffer[9];
  sprintf(tempBuffer, "%02d:%02d:%02d", TIMEhour, TIMEminute, TIMEseconds);
  return String(tempBuffer); 
}

String getTimeForWebFormatted() {
  //updateTime();  
  char tempBuffer[7];
  sprintf(tempBuffer, "%02d%02d%02d", TIMEhour, TIMEminute, TIMEseconds);
  return String(tempBuffer); 
}

void printTime() {  
  Serial.print("Time: ");
  Serial.println(getTimeFormatted());
}

void syncTime() {
  #ifdef WEB_ENABLE
    String tempTime = getWebTime();
    if (tempTime != "") {
      logMessage(LOGLEVEL_DEBUG, "TIME", "current time: ", getTimeFormatted());
      logMessage(LOGLEVEL_DEBUG, "TIME", "setting time to: ", tempTime);
      setTime(tempTime);
    }
  #endif
}

void setTime(String aTimeString) {
  logMessage(LOGLEVEL_DEBUG, "TIME", "set time to ", aTimeString);
  
  TIMEhour = _convertTimePartToValue(aTimeString.substring(0,2));
  TIMEminute = _convertTimePartToValue(aTimeString.substring(2,4));
  TIMEseconds = _convertTimePartToValue(aTimeString.substring(4,6));  
  TIMEtimeSyncPoint = seconds();

  printTime();
}

long seconds() {
  return millis()/1000;
}

void updateTimeDelay() {
  unsigned long tempCurrentMillis = millis();  
  if((tempCurrentMillis - TIMEPreviousMillis) > TIME_DELAY || TIMEPreviousMillis == 0) {   
    TIMEPreviousMillis = tempCurrentMillis;  
    _updateTime(); 
  }
}

/*************************************************************
 *
 * intern
 *
 *************************************************************/

void _updateTime() {
  unsigned long tempCurrentMillis = millis();
  if ((tempCurrentMillis - TIMEpreviousSyncMillis) > TIME_SYNC_INTERVAL ) {
    logMessage(LOGLEVEL_DEBUG, "TIME", "current time: ", getTimeFormatted());
    TIMEpreviousSyncMillis = tempCurrentMillis;
    syncTime(); 
    return;
  }

  unsigned long tempTimeAsSeconds = ((long)TIMEhour * 60 * 60);
  tempTimeAsSeconds = ((long)TIMEminute * 60) + tempTimeAsSeconds;
  tempTimeAsSeconds = (long)TIMEseconds + tempTimeAsSeconds;  

  unsigned long tempCurrentSeconds = seconds();
  tempTimeAsSeconds += tempCurrentSeconds - TIMEtimeSyncPoint;
  TIMEtimeSyncPoint = tempCurrentSeconds;
  
  TIMEseconds = (tempTimeAsSeconds) % 60;
  TIMEminute = (tempTimeAsSeconds/60) % 60;
  TIMEhour = (tempTimeAsSeconds/60/60) % 24;
}

int _convertTimePartToValue(String aValue) {
  char tempChars[3];
  aValue.toCharArray(tempChars,3);  
  return atoi(tempChars);
}

#endif
